package com.corpdata.system.dic.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;

import com.corpdata.system.dic.dao.SysDataDicDao;
import com.corpdata.system.dic.entity.SysDataDic;
import com.corpdata.system.dic.service.SysDataDicService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.corpdata.common.utils.UUIDUtil;
import com.corpdata.common.result.Result;
import com.alibaba.fastjson.JSON;
import com.corpdata.common.api.redis.RedisService;
import com.corpdata.common.result.DataGrid;
import com.corpdata.common.result.util.ResultUtil;
import com.corpdata.security.shiro.util.UserUtil;

/**
 * 
 * 
 * @author zealon
 * @email zealon@126.com
 * @date 2018-05-02 10:51:26
 */
@Service
public class SysDataDicServiceImpl implements SysDataDicService {

	@Autowired
	private SysDataDicDao sysDataDicDao;
	
	@Autowired
	private RedisService redisService;
	
	@Override
	public SysDataDic selectById(String id){
		return sysDataDicDao.selectById(id);
	}
	
	/**
	 * 查询分页数据列表
	 * 
	 * @param dg：数据列表对象
	 * @return：DataGrid
	 */
	@Override
//	@Cacheable(value="dics",cacheManager = "redisCacheManagerString")
	public DataGrid<SysDataDic> selectDataGrid(DataGrid dg) {
		PageHelper.startPage(dg.getPage(), dg.getSize());
		Page<SysDataDic> rows = sysDataDicDao.selectList(dg.getSearchParams());
		dg.setTotalAndRows(rows.getTotal(), rows);
		return dg;
	}
	
	/**
	 * 查询数据列表
	 * 
	 * @param params：数据列表对象
	 * @return
	 */
	public Page<SysDataDic> selectList(String dicType) {
		Map<String, Object> params = new HashMap<String,Object>();
		params.put("dicType", dicType);
		return sysDataDicDao.selectList(params);
	}
	
	@Cacheable(value="dics_json",cacheManager = "redisCacheManagerString",key="#p0")
	public String selectJson(String dicType){
		List<SysDataDic> list = selectList(dicType);
		return JSON.toJSONString(list);
	}
	
	/**
	 * 查询总数量
	 * 
	 * @param params:参数集
	 * @return：Internet
	 */
	@Override
	public int selectCount(Map<String, Object> params){
		return sysDataDicDao.selectCount(params);
	}
	
	/**
	 * 新增数据
	 * 
	 * @param sysDataDic
	 * @return
	 */
	@Override
	@CacheEvict(value="dics_json", allEntries=true)
	public Result insert(SysDataDic sysDataDic){
		Date date = new Date();
		sysDataDic.setId(UUIDUtil.getUUID());
		sysDataDic.setCreated(date);
		sysDataDic.setModified(date);
		sysDataDic.setCreater(UserUtil.getCurrentUserid());
		if(sysDataDicDao.insert(sysDataDic)>0){
			return ResultUtil.success();
		}else{
			return ResultUtil.fail();
		}
	}
	
	/**
	 * 修改数据
	 * 
	 * @param sysDataDic
	 * @return
	 */
	@Override
	@CacheEvict(value="dics_json", allEntries=true)
	public Result update(SysDataDic sysDataDic){
		sysDataDic.setModified(new Date());
		if(sysDataDicDao.update(sysDataDic)>0){
			return ResultUtil.success();
		}else{
			return ResultUtil.fail();
		}
	}
	
	/**
	 * 按id删除数据
	 * 
	 * @param id
	 * @return
	 */
	@Override
//	@CacheEvict(value="dics_json", allEntries=true)
	public Result delete(String id,String dicType){
		if(sysDataDicDao.delete(id)>0){
			redisService.delete("dics_json:"+dicType);
			return ResultUtil.success();
		}else{
			return ResultUtil.fail();
		}
	}
	
	/**
	 * 按ids批量删除数据
	 * 
	 * @param id
	 * @return
	 */
	@Override
	@CacheEvict(value="dics", allEntries=true)
	public Result batchDelete(String[] ids){
		if(sysDataDicDao.batchDelete(ids)>0){
			return ResultUtil.success();
		}else{
			return ResultUtil.fail();
		}
	}
	
}
